package graduation.project.testcase.strategy;

import graduation.project.body.OriginExpression;
import graduation.project.body.TestCase;
import graduation.project.body.TestCases;
import graduation.project.exception.NonExpressionException;
import graduation.project.utilities.Utility;

import java.util.ArrayList;

public class MUTPGenerator implements TestSetGenerator {

	public TestCases generateTestCases(String expression, int testCaseNumber) {
		try {
			OriginExpression originEx;
			originEx = new OriginExpression(expression);
			return generateTestCases(originEx, testCaseNumber);
		} catch (NonExpressionException e) {
			System.out.println("the expression is not valid boolean expression");
			e.printStackTrace();
		}
		return null;
	}

	public TestCases generateTestCases(OriginExpression expression,
			int testCaseNumber) {
		
		TestCases result = new TestCases(expression.getExpression(), expression.getModel(), "MUTP");
		ArrayList<TestCase> testSet = new ArrayList<TestCase>();
		
		int[] maxdiff;
		int[][] utpSet = expression.getUtpSet();
		for(int i = 0; i < utpSet.length; i++){
			//add here
			if(utpSet[i].length <= 1){
				testSet.add(new TestCase(utpSet[i][0], true));
			}else{				
				maxdiff = Utility.getMaxDifferCells(utpSet[i]);
				int or1 = maxdiff[0];
				int and1 = or1;
				int or2 = maxdiff[1];
				int and2 = or2;
				testSet.add(new TestCase(or1, true));
				testSet.add(new TestCase(or2, true));
				for(int j = 1; j < utpSet[i].length; j++){
					if(((or1 != (or1 | utpSet[i][j])) && (or2 != (or2 | utpSet[i][j]))) || 
							((and1 != (and1 & utpSet[i][j])) && (and2 != (and2 & utpSet[i][j])))){
						testSet.add(new TestCase(utpSet[i][j], true));
						and1 = and1 & utpSet[i][j];
						or1 = or1 | utpSet[i][j];
						and2 = and2 & utpSet[i][j];
						or2 = or2 | utpSet[i][j];
					}
				}
			}
		}		
		result.setTestSet(testSet);
			
		return result;
	}
	
	public static void main(String [] args){
		System.out.println("A1");
		AssuranceBasedGenerator mutpG = new AssuranceBasedGenerator();
		TestCases tcs = mutpG.generateTestCases("ab + cd", 123);
		ArrayList<TestCase> testSet = tcs.getTestSet();
		
		System.out.println("A2");
		if(testSet == null)
			System.out.println("the test Set is null");
		for(int i = 0; i < testSet.size(); i++)
			System.out.println("" + i + ": " + testSet.get(i).getTestData() + ", " + testSet.get(i).getExpectantResult());
		System.out.println("A3");
	}
}
